package io.mft.datasource.db.accessor.impl;

import io.mft.commons.db.SQL;
import io.mft.commons.exception.MFTException;
import io.mft.commons.function.ExecuteReturn;
import io.mft.commons.function.response.kv.KVMap;
import io.mft.commons.function.response.kv.PageObject;
import io.mft.datasource.db.accessor.DataAccessor;
import io.mft.datasource.db.accessor.bean.DAContext;
import io.mft.datasource.db.accessor.bean.DAResponse;
import io.mft.datasource.db.accessor.bean.DbContextValue;
import io.mft.datasource.db.accessor.sql.SQLScript;
import io.mft.datasource.db.accessor.sql.SQLScriptManager;
import io.mft.datasource.db.accessor.util.PageLoader;
import io.mft.datasource.db.accessor.util.ProcedureFunctionSQLParameterTypeBuilder;

/**
 * 数据库分页操作
 * @说明：TODO
 * @类型名称：DbPageAccessor
 * @创建者: 胡常建
 * @创建时间: 2021年1月28日 下午5:05:33
 * @修改者: 胡常建
 * @修改时间: 2021年1月28日 下午5:05:33
 */
public class DbPageAccessor extends DataAccessor<DbContextValue> {

	@Override
	public ExecuteReturn doExecute(DAContext<DbContextValue> context) {
		try {
			DbContextValue dbContextValue = context.getContextValue();
			SQLScript sqlScript = SQLScriptManager.getInstance().get(dbContextValue.getScriptKey());
			int pageNo = context.getRequest().getRequestValue().getPageNo();
			int pageSize = context.getRequest().getRequestValue().getPageSize();
			DAResponse daResponse = context.getResponse();
			switch (sqlScript.getType()) {
			case table:
				PageObject<KVMap> pageObject = PageLoader.page(dbContextValue.getDatasourceId(), sqlScript.getScript(),
						context.getRequest().getRequestValue(), pageNo, pageSize);
				daResponse.result(pageObject);
				break;
			case sql:
				SQL sql = SQLScriptManager.getInstance().loadSQL(dbContextValue.getScriptKey(), context.getRequest().getRequestValue());
				PageObject<KVMap> sqlpageObject = PageLoader.pageBysql(dbContextValue.getDatasourceId(), sql.getSql(),
						sql.getArgs(), pageNo, pageSize);
				daResponse.result(sqlpageObject);
				break;
			case sqlfunction:
				SQL sqlfunction = SQLScriptManager.getInstance().loadSQL(dbContextValue.getScriptKey(), context.getRequest().getRequestValue());
				String[] paramTypes=ProcedureFunctionSQLParameterTypeBuilder.build(sqlfunction, context.getRequest().getRequestValue());
				PageObject<KVMap> sqlfunctionpageObject=PageLoader.pageBysqlprocedure(dbContextValue.getDatasourceId(), sqlfunction.getSql(),
						paramTypes,sqlfunction.getArgs(), pageNo, pageSize);
				daResponse.result(sqlfunctionpageObject);
				break;				
			case sqlprocedure:
				SQL sqlprocedure = SQLScriptManager.getInstance().loadSQL(dbContextValue.getScriptKey(), context.getRequest().getRequestValue());
				String[] pparamTypes=ProcedureFunctionSQLParameterTypeBuilder.build(sqlprocedure, context.getRequest().getRequestValue());
				PageObject<KVMap> sqlprocedurepageObject=PageLoader.pageBysqlprocedure(dbContextValue.getDatasourceId(), sqlprocedure.getSql(),
						pparamTypes,sqlprocedure.getArgs(), pageNo, pageSize);
				daResponse.result(sqlprocedurepageObject);
				break;
			default:
				throw new MFTException(-1, "不支持的SQLScript类型");
			}
			ExecuteReturn executeReturn=ExecuteReturn.builder().build();
			return executeReturn; 
		} catch (Exception e) {
			throw new MFTException(e);
		}
	}

}
